package com.warrior.gatherclient.service.impl;

import com.warrior.gatherclient.grpc.Messages;
import com.warrior.gatherclient.service.FileSystemService;
import com.warrior.gatherclient.util.CommonUtil;
import com.warrior.gatherclient.service.SystemInfoService;
import com.warrior.gathercommon.constants.CommonStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.software.os.FileSystem;
import oshi.software.os.OSFileStore;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName FileSystemServiceImpl.java
 * @Author xujian
 * @Description
 * @CreateTime 2020年12月17日 15:27:00
 */
@Service
@Slf4j
public class FileSystemServiceImpl implements FileSystemService {

    @Autowired
    private SystemInfoService systemInfoService;

    @Override
    public String getServiceKey() {
        return CommonStatus.CONSTANT_FILESYSTEM;
    }

    @Override
    public Messages.Builder execute() {
        Messages.Builder messages = CommonUtil.initMessage(CommonStatus.CONSTANT_FILESYSTEM);
        SystemInfo systemInfo = systemInfoService.getSystemInfo();
        FileSystem fileSystem = systemInfo.getOperatingSystem().getFileSystem();
        long maxFileDescriptors = fileSystem.getMaxFileDescriptors();
        //系统最大文件数
        Messages.Metric.Builder builder = Messages.Metric.newBuilder();
        builder.setMetricName(CommonStatus.CONSTANT_FILESYSTEM_FILES);
        builder.setMetricVaule(String.valueOf(maxFileDescriptors));
        messages.addMetrics(builder);

        //磁盘使用情况
        List<OSFileStore> fileStores = fileSystem.getFileStores();
        for (OSFileStore fileStore : fileStores) {
            String name = fileStore.getName();
            BigDecimal total = new BigDecimal(fileStore.getTotalSpace()).divide(new BigDecimal(1024 * 1024 * 1024), 2, BigDecimal.ROUND_FLOOR);
            Messages.Metric.Builder builderTotal = Messages.Metric.newBuilder();
            builderTotal.setMetricName(CommonStatus.CONSTANT_FILESYSTEM_TOTAL);
            builderTotal.setMetricVaule(name + CommonStatus.CONSTANT_JOINTSTR + total.doubleValue());
            messages.addMetrics(builderTotal);

            BigDecimal free = new BigDecimal(fileStore.getUsableSpace()).divide(new BigDecimal(1024 * 1024 * 1024), 2, BigDecimal.ROUND_FLOOR);
            Messages.Metric.Builder freeBuilder = Messages.Metric.newBuilder();
            freeBuilder.setMetricName(CommonStatus.CONSTANT_FILESYSTEM_FREE);
            freeBuilder.setMetricVaule(name + CommonStatus.CONSTANT_JOINTSTR + free.doubleValue());
            messages.addMetrics(freeBuilder);

            //已用
            BigDecimal used = total.subtract(free);
            Messages.Metric.Builder usedBuilder = Messages.Metric.newBuilder();
            usedBuilder.setMetricName(CommonStatus.CONSTANT_FILESYSTEM_USED);
            usedBuilder.setMetricVaule(name + CommonStatus.CONSTANT_JOINTSTR + used.doubleValue());
            messages.addMetrics(usedBuilder);
        }

        return messages;
    }

}
